package com.example.springboot02amqp.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: 喵喵~
 * Date: 2022/4/18
 * Time: 15:21
 * Description:
 */
@RestController
public class FileController {
    /**
     * @Description: 图片上传
     * @Author: duanweijie
     * @Date: 2020/5/12
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("image") MultipartFile file) {
        //方法1
        String path = "D:/file/src";
        String targetPath = path + "/" + file.getOriginalFilename();
        //这个是文件的路径
        File filePath = new File(path); //这个是存放文件的目录
        while (!filePath.exists()) {
            filePath.mkdirs();
        }
        try {
            file.transferTo(new File(targetPath));
            //注意此时的filePath是目标文件夹+文件名字，切记！！不要只搞成目标文件夹了    }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
